﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Cemit.PolyServer
{
    /// <summary>
    /// 提供使用Server对象获取配置值的扩展
    /// </summary>
    [Obsolete("Server没有获取配置的职责，请使用Jsons.Get().Read()获取配置项")]
    public static class ServerConfigReadExtensions
    {
        [Obsolete("Server没有获取配置的职责，请使用Jsons.Get()获取配置项")]
        public static IPolyServer AddConfigRead(
            this IPolyServer server, out string value, IConfigs configs, string key)
        {
            return AddConfigRead(server, out value, configs, key, s => s);
        }

        [Obsolete("Server没有获取配置的职责，请使用Jsons.Get()获取配置项")]
        public static IPolyServer AddConfigRead(
          this IPolyServer server, out string[] values, IConfigs configs, string key)
        {
            return AddConfigRead(server, out values, configs, key, s => s);
        }

        [Obsolete("Server没有获取配置的职责，请使用Jsons.Get()获取配置项")]
        public static IPolyServer AddConfigRead<T>(
            this IPolyServer server, out T[] values, IConfigs configs, string key, Func<string, T> converter)
        {
            List<T> outs = new List<T>();
            foreach (var item in configs.GetValues(key))
            {
                outs.Add(converter(item));
            }
            values = outs.ToArray();
            return server;
        }

        [Obsolete("Server没有获取配置的职责，请使用Jsons.Get()获取配置项")]
        public static IPolyServer AddConfigRead<T>(
            this IPolyServer server, out T value, IConfigs configs, string key, Func<string, T> converter)
        {
            value = converter(configs.GetValue(key));
            return server;
        }
    }
}
